﻿
// First: Create config
ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null,
    decorateElement: true,
    errorClass: "error",
    errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.UserTransaction = ko.observableArray(data.UserTransaction);
    this.TransactionId = ko.observable(0);
    this.Total = ko.observable(0);
    this.Visible = ko.observable(true);
    this.VIP = ko.observable(data.UserInfo.VIP);

    // Edit item
    this.EditItems = function (item) {
        viewModel.TransactionId(item.TransactionId);
        viewModel.Total(item.Total);
        viewModel.Visible(item.Visible);
    };

    // Delete Item
    this.DeleteItems = function (item) {
        // Call an ajax function
        var dataPost = { "userId": userViewId, "id": item.TransactionId };
        $.ajax({
            type: "POST",
            url: "/Management/AccountList/DeleteTransaction",
            data: dataPost,
            dataType: "json",
            success: function (data, textStatus, jqXHR) {
                // Stop progress bar
                hideProcess();

                // Set values
                viewModel.TransactionId(0);
                viewModel.Total(0);
                viewModel.UserTransaction(data.UserTransaction);
            },
            error: function (xhr, status, error) {
                // Stop progress bar
                hideProcess();
                showPopupError();
            }
        });
    };

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            openProcess();

            // Call an ajax function
            var dataPost = { "userId": userViewId, "id": this.TransactionId(), "total": this.Total(), "visible": this.Visible(), "vip": this.VIP() };
            $.ajax({
                type: "POST",
                url: "/Management/AccountList/UpdateTransaction",
                data: dataPost,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    // Stop progress bar
                    hideProcess();

                    // Set values
                    viewModel.TransactionId(0);
                    viewModel.Total(0);
                    viewModel.UserTransaction(data.UserTransaction);
                },
                error: function (xhr, status, error) {
                    // Stop progress bar
                    hideProcess();
                    showPopupError();
                }
            });
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));